<?php
/**
 * Created by PhpStorm.
 * User: xiaozhu
 * Date: 19-1-10
 * Time: 下午6:08
 */

namespace App\Utils;

class Page {
    public $firstRow; // 起始行数
    public $firstPage = 1;//首页
    public $listRows; // 列表每页显示行数
    public $totalRows; // 总行数
    public $totalPages; // 分页总页面数
    public $rollPage   = 10;// 分页栏每页显示的页数
    public $lastSuffix = false; // 最后一页是否显示总页数
    public $urlSelf	 = '';       //拼接 url
    private $url     = ''; //当前链接URL
    private $nowPage = 1;

    // 分页显示定制
    private $config  = array(
        'header' => '<span>共 %TOTAL_ROW% 条记录</span>',
        'cfirstpage'=> '首页',
        'prev'   => '上一页',
        'next'   => '下一页',
        'first'  => '1',
        'last'   => '尾页',
//        'theme'  => '第 %NOW_PAGE% 页 每页 %NOWPAGE% 条 %FIRSTPAGE% %UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%',
        'theme'  => '%FIRSTPAGE% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%  %TOTAL_ROW% ',
    );

    /**
     * 架构函数
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     */
    public function __construct($totalRows, $listRows, $urlSelf, $getPage, $action) {
        /* 基础设置 */
        $this->totalRows  = $totalRows; //设置总记录数
        $this->listRows   = $listRows;  //设置每页显示行数
        $this->nowPage    = empty($getPage) ? 1 : intval($getPage);
        $totalPage = ceil($totalRows / $listRows);
        if ($this->nowPage < 1) {
            $this->nowPage = 1;
        } else if ($this->nowPage > $totalPage) {
            $this->nowPage = $totalPage;
        }
        $left_page = $totalPage - $this->nowPage;

        if($getPage > $totalPage)  abort(404);              // 大于总页数  返回404

        if ($this->nowPage > 5 && $totalPage > 10) {
            if ($left_page > 5) {
                $this->rollPage = 11;
            } else if ($left_page <= 5) {
                $this->rollPage = $left_page + 6;
            }
        }

        $this->firstRow   = $this->listRows * ($this->nowPage - 1);
        $this->firstPage  = $this->firstPage;
        $this->urlSelf    = $urlSelf;
        $this->action     = $action;
    }
    /**
     * 定制分页链接设置
     * @param string $name  设置名称
     * @param string $value 设置值
     */
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    /**
     * 生成链接URL
     * @param  integer $page 页码
     * @return string
     */
    private function url($nowPage){
        if($this->action == 'pinglun') {
            $pageUrl = $nowPage == 1 ? $this->url . '/' : $this->url . '_' . $nowPage . '.htm';
        } else if ($this->action == 'special') {
            $pageUrl = $nowPage == 1 ? $this->url . '/' : $this->url . '_' . $nowPage . '.html';
        } else {
            $pageUrl = $nowPage == 1 ? $this->url . '/' : $this->url . '/' . $nowPage . '/';
        }
        return  $pageUrl;
    }

    /**
     * 组装分页链接
     * @return string
     */
    public function show() {
        if(0 == $this->totalRows) return '';
        /* 生成URL */
        $this->url = '/' . $this->urlSelf;
        /* 计算分页信息 */
        $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
        if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        /* 计算分页临时变量 */
        $now_cool_page      = $this->rollPage/2;
        $now_cool_page_ceil = ceil($now_cool_page);
        $this->lastSuffix && $this->config['last'] = $this->totalPages;
        //首页
        $first_page = $this->firstPage;
        $judge_page = $this->nowPage;
        //$first_page_url = $judge_page == 1 ? "首页" : '<a href="' . $this->url($first_page) . '">' . $this->config['cfirstpage'] . '</a>';
        $up_row  = $this->nowPage - 1;
        if ($judge_page == 1) {
            $first_page_url = '<a>' . $this->config['cfirstpage'] . '</a>';
            $up_page = '<a>' . $this->config['prev'] . '</a>';
        } else {
            $first_page_url = '<a href="' . $this->url($first_page) . '">' . $this->config['cfirstpage'] . '</a>';
            $up_page = '<a href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>';
        }
        //下一页
        $down_row  = $this->nowPage + 1;
        if ($judge_page >= $this->totalPages) {
            $down_page = '<a>' . $this->config['next'] . '</a>';
            //最后一页
            $the_end = '<a>' . $this->config['last'] . '</a>';
        } else {
            $down_page = '<a href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>';
            $the_end = '<a href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>';
        }

        //第一页
        //$the_first = '';
        //if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
        //    $the_first = '<a href="' . $this->url(1) . '">' . $this->config['first'] . '</a>';
        //}



        $total_page = '<span>共' . $this->totalPages . '页</span>';
        //数字连接
        $link_page = "";
        if ($this->nowPage >= 10 && $this->totalPages > 10) {
            $link_page .= ' <a href="' . $this->url(1) . '">1</a> <a href="' . $this->url(2) . '">2</a> <a href="' . $this->url(3) . '">3</a> <span>......</span>';
        }
        for($i = 1; $i <= $this->rollPage; $i++){
            if(($this->nowPage - $now_cool_page) <= 0 ){
                $page = $i;
            }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
                $page = $this->totalPages - $this->rollPage + $i;
            }else{
                $page = $this->nowPage - $now_cool_page_ceil + $i;
            }

            if($page > 0 && $page != $this->nowPage){

                if($page <= $this->totalPages){
                    $link_page .= ' <a href="' . $this->url($page) . '">' . $page . '</a> ';
                }else{
                    break;
                }
            }else{
                if($page > 0){
                    $link_page .= " <a style='color:red;'>$page</a>&nbsp ";
                }
            }
        }
        //替换分页内容
        $page_str = str_replace(
            array('%FIRSTPAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%'),
            array($first_page_url, $up_page, $down_page, $link_page, $the_end, $total_page),
            $this->config['theme']
        );
        return "{$page_str}";
    }
}
